<?php
namespace app\admin\controller;


use think\Controller;
use think\Db;
use app\admin\validate\Cate as CateValidate; 
use cate\Category;
class Goods extends Controller
{
    /*
     * 商品列表
     */
    public function lst(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $goodsres=db('goods')->alias('a')->field(['a.*','b.cate_name'])->join('cate b','a.cate_id=b.id')->paginate(6);
        //dump($goodsres);
        $this->assign('goodsres',$goodsres);
        return view('list');
    }

    /*
     * 添加商品
     */
    public function add(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
       if(request()->isPost()){
           $data=input('post.');
           if(isset($data['onshelf'])){
            $data['onshelf']=1;
           }else{
               $data['onshelf']=0;
           }
          model('goods')->save($data);
           $this->success("添加商品成功",'lst');
       }
       $cate=db('cate')->select();
       $category= new Category();
       $cate=$category->cateTree($cate);
       $this->assign('cate',$cate);
        return view();
    }

    /*
     * 修改商品
     */
    public function edit($id){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        if(request()->isPost()){
            $data=input('post.');
            if(isset($data['onshelf'])){
                $data['onshelf']=1;
            }else{
                $data['onshelf']=0;
            }
            \db('goods')->where('id',$data['id'])->update([
                'goods_name'=>$data['goods_name'],
                'description'=>$data['description'],
                'thumb'=>$data['thumb'],
                'market_price'=>$data['market_price'],
                'sale_price'=>$data['sale_price'],
                'cate_id'=>$data['cate_id'],
                'reserve'=>$data['reserve'],
                'onshelf'=>$data['onshelf']
            ]);
            \db('goodsattr')->where('goods_id',$data['id'])->update([
                'manufactor'=>$data['manufactor'],
                'qualityday'=>$data['qualityday'],
                'weight'=>$data['weight'],
                'unit'=>$data['unit'],
                'flavor'=>$data['flavor']
            ]);
        
            $this->success('修改商品成功！','lst');
        }



        $goods=\db('goods')->where('id',$id)->find();
        $attrs=\db('goodsattr')->where('goods_id',$id)->find();
        $cate=db('cate')->select();
        $category= new Category();
        $cate=$category->cateTree($cate);
        $this->assign([
            'cate'=>$cate,
            'goods'=>$goods,
            'attrs'=>$attrs
        ]);
        return view();
    }



    /*
     *删除商品
     */
    public function del($id){
        
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $data=\db('goods')->field('thumb')->where('id',$id)->find();
        //file_exists('/')
        //dump($data['thumb']);
        $path='../public/static/admin/uploads/'.$data['thumb'];
        if(file_exists($path)){
            @unlink($path);
        }
        \app\admin\model\Goods::destroy($id);
        $this->success('删除商品成功','lst');
    }

    public function uploads()//上传模块
    {
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('../public/static/admin/uploads');
        if(!$info){
            $this->error($file->getError());
        }else{
            $file_a= $info->getFilename();
            echo '{"code":0,"msg":"成功上传","url":"'.$file_a.'","date":"'.date("Ymd").'"}';
        }
        unset($info);
    }



    
}